home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / ast_comp / cpp-kit.lha / c++kit / Scan.H < prev    next >
C/C++ Source or Header  |  1993-04-11  |  3KB  |  129 lines

  1. /* 9th April, 1993 Mayan Moudgill
  2.  * Basically is given a file. If asked it will try and scan one of the
  3.  * following tokens from the file.
  4.  *    strings:    "([^"\\\n]|(\.))*"
  5.  *    identifier: [A-Za-z_0-9]+
  6.  *    integer:    [-+]?[0-9]+
  7.  *    character:  .
  8.  * These returns a Token.
  9.  * It can also accept a mark, which returns a Mark, and a reject which
  10.  * when given a Mark, rolls back the state to the Mark, and starts matching
  11.  * from that point onwards. Also, for convenience it can rollback the
  12.  * last token. Also, it can match a character and a character-string.
  13.  * (these are equivalent to trying to scanning a character/string,
  14.  * and rolling back if the character/string does not exactly match the
  15.  * argument).
  16.  *
  17.  * The other facility that the function provides is that it keeps track
  18.  * of the number of lines read in.
  19.  *
  20.  * I can think of at least of two ways of implementing the tokenizing
  21.  * and rollback mechanisms: reading files, and mmap'ing them. This
  22.  * implementation uses mmap.
  23.  */
  24.  
  25. #include <osfcn.h>
  26. #include <stdlib.h>
  27. #include <iostream.h>
  28.  
  29. #include "Token.H"
  30.  
  31. class Scan {
  32. public:
  33.     class Mark {
  34.        friend class Scan;
  35.     private:
  36.        int     _nl;
  37.        char *  _at;
  38.     };
  39. private:
  40.    char      _name[256];  // file name
  41.    int       _fd;         // file descriptor
  42.    char *    _file;       // the pointer to the beginning of the mmap'd region
  43.    long      _size;       // the file size
  44.    char *    _end;        // end of file
  45.  
  46.    int       _close;      // has it not been opened?
  47.    int       _eof;        // seen eof?
  48.    char *    _at;         // file pointer
  49.    int       _nl;         // new lines read in
  50.    char *    _oat;        // previous file pointer (for rollback)
  51.    int       _onl;        // previous new lines
  52.  
  53.    char *    _wat;
  54.    char *    _tat;
  55.    int       _wnl;
  56.    int       _tnl;
  57.  
  58. private:
  59.    int      _start()
  60.       {
  61.      _wat = _tat = _at;
  62.      _wnl = _tnl = _nl;
  63.      return _at == _end || _close;
  64.       }
  65.    int      _get()
  66.       {
  67.       int  c;
  68.      if( _tat == _end ) {
  69.         c = EOF;
  70.      }
  71.      else {
  72.         c = *_tat++;
  73.         if( c == '\n' ) {
  74.            _tnl++;
  75.         }
  76.      }
  77.          return c;
  78.       }
  79.    void      _backup()
  80.       {
  81.      _tat--;
  82.      if( *_tat == '\n') {
  83.         _tnl--;
  84.      }
  85.       }
  86.    void      _commit()
  87.       {
  88.       _oat = _at;
  89.       _onl = _nl;
  90.       _at = _tat;
  91.       _nl = _tnl;
  92.       }
  93.    int      _space();
  94. public:
  95.             Scan();
  96.             Scan(char * name);
  97.             ~Scan();
  98.    int      line()
  99.       { return _nl; }
  100.    Mark     mark()
  101.       {
  102.       Mark mark;
  103.      mark._nl = _nl;
  104.      mark._at = _at;
  105.      return mark;
  106.       }
  107.    void     back( Mark& mark)
  108.       {
  109.      _onl = _nl = mark._nl;
  110.      _oat = _at = mark._at;
  111.       }
  112.    void     back()
  113.       {
  114.      _nl = _onl;
  115.      _at = _oat;
  116.       }
  117. /* now for the actual scan routines */
  118. /* scan_char & scan_string are special cases */
  119.    int      match(char c, Token& result);
  120.    int      match(char *, Token&);
  121.  
  122.    int      number(Token&);
  123.    int      string(Token&);
  124.    int      identifier(Token&);
  125.    int      token(Token&);
  126.    int      character(Token& result);
  127.    int      eof();
  128. };
  129.